﻿using Controller.Enum;
using Controller.Model;
using DPS.ControllerClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PickingGuide.Handle
{
    public class AioiControllerHandler
    {
        public string _Ip;
        public int _Port;
        /// <summary>
        /// 获取控制器
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        /// 

        public Action<TriggerLabelPacketModel> ButtonEvent;

        public BaseController GetController()
        {
            BaseController controller = ControllerFactory.GetInstance().Get("127.0.0.1", 9008, DeviceButtonEvent, DeviceExceptionEvent,
                DeviceStateEvent, ControllerConnectionStateChangeEvent, ControllerManagerStateEvent);
            return controller;
        }

        /// <summary>
        /// 控制器触发设备按钮事件
        /// </summary>
        /// <param name="iModel"></param>
        protected void DeviceButtonEvent(TriggerLabelPacketModel iModel)
        {
            ButtonEvent?.BeginInvoke(iModel, null, null);
            if (!string.IsNullOrEmpty(iModel.EpcData))
            {
                //Console.WriteLine(iModel.EpcData);//E20000006A130084100035F6
                //GetController().Control(new CancelPacketModel
                //{
                //    AddressList = new List<string> { iModel.Address },
                //    ControllerKey = iModel.ControllerKey,
                //    EquipmentBrand = EquipmentBrandEnum.AIOI,
                //    LabelType = EquipmentLabelTypeEnum.AIOI_LABEL_DEFAULT
                //});
            }
        }

        /// <summary>
		/// 控制器的设备异常事件
		/// </summary>
		/// <param name="iKey"></param>
		/// <param name="iDeviceAddress"></param>
		/// <param name="iExceptionCode"></param>
		protected void DeviceExceptionEvent(string iKey, string iDeviceAddress, ControllerExceptionCodeEnum iExceptionCode)
        {

        }

        /// <summary>
		/// 控制器设备状态事件
		/// </summary>
		/// <param name="iModel"></param>
		protected void DeviceStateEvent(LabelStatePacketModel iModel)
        {
        }

        /// <summary>
        /// 控制器连接状态修改事件
        /// </summary>
        /// <param name="iModel"></param>
        internal void ControllerConnectionStateChangeEvent(ControllerStatePacketModel iModel)
        {
        }

        /// <summary>
		/// 控制器管理器连接状态修改事件
		/// </summary>
		/// <param name="iState"></param>
		internal void ControllerManagerStateEvent(bool iState)
        {

        }
    }
}
